﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;
using LogLib;

namespace PrintingServer
{
    internal class IniFile
    {
        static string IniFileName = "Server.ini";
        static string Section = "config";
        static IniFile()
        {
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
            directory = directory.Substring(6);
            IniFileName = Path.Combine(directory, "Server.ini");
        }

        public static int LoadInt32Value(string field, int defVal)
        {
            try
            {
                string strVal = IniValue(field);
                return Convert.ToInt32(strVal);
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
                return defVal;
            }
        }

        public static bool LoadBoolenValue(string field, bool defVal)
        {
            try
            {
                bool val = Convert.ToBoolean(IniValue(field));
                return val;
            }
            catch (Exception ex)
            {
                string error = string.Format("配置项[{0}]加载错误: {1}", field, ex.Message);
                Log.WriteAsync(error);
                Log.WriteException(ex.ToString());
                return defVal;
            }
        }

        #region 读取INI文件
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static string IniValue(string IniKey)
        {        
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, IniKey, "", temp, 500, IniFileName);
            return temp.ToString();
        }

        /// <summary>
        /// 读取INI文件(找不到配置文件返回默认值)
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static string IniValue(string IniKey, string strDefault)
        {         
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, IniKey, strDefault, temp, 500, IniFileName);
            return temp.ToString();
        }

        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        #endregion

        #region 写INI文件
        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static long WriteValue(string IniKey, string NewValue)
        {          
            long i = WritePrivateProfileString(Section, IniKey, NewValue, IniFileName);
            return i;
        }

        [DllImport("kernel32")]
        public static extern long WritePrivateProfileString(
            string section, string key, string val, string filePath);
        #endregion
    }
}